import { queryString,getCookie } from '..//tools.js';
import { APIDetail,APICart} from '..//fetch.js';

$('#head').load('../dewu/head.html');
$('#foot').load('../dewu/foot.html');

var gid = queryString('gid');

APIDetail({
    gid
}).then(({ data: { gname, desc, price, detail, imgs } }) => {

    // console.log(detail);
    var html = '';
    $('.detail h2').html(gname);
    $('.detail-a').html(desc);
    $('.detail-b').html(price);
    $('#main').html(detail);

       $.each(JSON.parse(imgs),function(index,item){
           if(index == 0) $('.aaa img').attr('src',item)
           if(index == 0) $('.right img').attr('src',item)
    html +=` 
    <img src="${item}" alt="" class="st">
   
    `
       })
    $('.box .box-img').html(html);

    layui.use(['layer'], function () {
        var layer = layui.layer;


        $('#cart').bind('click', function () {
            //    console.log(gid);
            var username = getCookie('login_user');
            if(!username){
                 sessionStorage.loginBack =location.href
                location.href = '../dewu/login.html';
            }
            APICart({
                gid,
                username,
                img: $(' .right img').attr('src'),
                gname: $('.detail h2').html(),
                price: $('.detail-b').html(),
                count: $('.count-input').val(),
            }).then(({ code, msg }) => {
                if ({code}) {
                    layer.msg('加入成功')
                    location.href ='../dewu/cart.html'
                } else {
                    layer.msg(msg);
                }
               
            })
        })
    })
})

$(document).on('mouseover', '.box-img img', function () {
    $('.aaa img').attr('src', $(this).attr('src'));
    $('.right img').attr('src',$(this).attr('src'))
})
